#include "player.h"

Player::Player() : SDLGameObject() { }

void Player::Draw() {
    SDLGameObject::Draw();
}

void Player::Update() {
    velocity_.setx(0);
    velocity_.sety(0);
    HandleInput_();
    acceleration_.setx(0);
    current_frame_ = (int)(SDL_GetTicks() / 100 % num_frames_);
    SDLGameObject::Update();
}

void Player::Clean() {

}

void Player::Load(const LoadParams* params) {
    SDLGameObject::Load(params);
}

void Player::HandleInput_() {
    if (TheIH::instance().JoysticksInitialezed()) {
        // left(1) and right(2) stick of joy 0 (direction X)
        if (TheIH::instance().Xvalue(0, 1) > 0 ||
            TheIH::instance().Xvalue(0, 1) < 0) {
            velocity_.setx(1 * TheIH::instance().Xvalue(0, 1));
        }

        if (TheIH::instance().Xvalue(0, 2) > 0 ||
            TheIH::instance().Xvalue(0, 2) < 0) {
            velocity_.setx(1 * TheIH::instance().Xvalue(0, 2));
        }

        // left(1) and right(2) stick of joy 0 (direction Y)
        if (TheIH::instance().Yvalue(0, 1) > 0 ||
            TheIH::instance().Yvalue(0, 1) < 0) {
            velocity_.sety(1 * TheIH::instance().Yvalue(0, 1));
        }

        if (TheIH::instance().Yvalue(0, 2) > 0 ||
            TheIH::instance().Yvalue(0, 2) < 0) {
            velocity_.sety(1 * TheIH::instance().Yvalue(0, 1));
        }
    }

    // test joystick button
    /*
    if (TheIH::instance()->get_button_states(0, 3)) {
        velocity_.setx(1);
    }
    */

    // test mouse button
    /*
    if (TheIH::instance()->get_mouse_button_states_(kLeft)) {
        velocity_.setx(1);
    }
    */

    // test mouse motion
    /*
    Vector2D* vec = TheIH::instance()->get_mouse_position();
    velocity_ = (*vec - position_) / 100;
    */

    Vector2D* target = TheIH::instance().get_mouse_position();
    velocity_ = *target - position_;
    velocity_ /= 50;

    // test key down
    if (TheIH::instance().IsKeyDown(SDL_SCANCODE_W)) {
        velocity_.sety(-2);
    }
    if (TheIH::instance().IsKeyDown(SDL_SCANCODE_S)) {
        velocity_.sety(2);
    }
    if (TheIH::instance().IsKeyDown(SDL_SCANCODE_A)) {
        velocity_.setx(-2);
    }
    if (TheIH::instance().IsKeyDown(SDL_SCANCODE_D)) {
        velocity_.setx(2);
    }

}

